package controller;

/*
 * Action for creating a new survey as a researcher.
 */

import helper.QuestionType;
import helper.UserType;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;

import databeans.EntityClass;
import databeans.MailingList;
import databeans.Question;
import databeans.Survey;
import databeans.User;
import model.EntityClassDAO;
import model.MailingListDAO;
import model.Model;
import model.QuestionDAO;
import model.SurveyDAO;

public class CreateSurveyAction extends Action {
	private QuestionDAO questionDAO;
	private SurveyDAO surveyDAO;
	private EntityClassDAO ecDAO;
	private MailingListDAO mlDAO;
	
	public CreateSurveyAction(Model model) {
		questionDAO = model.getQuestionDAO();
		surveyDAO = model.getSurveyDAO();
		ecDAO = model.getEntityClassDAO();
		mlDAO = model.getMailingListDAO();
	}
	
    public String getName() {
		return "createsurvey.do";
	}
    
    public String perform(HttpServletRequest request) {

        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
    	// First, validate whether researcher is accessing this
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	
    	// Validate the survey number
    	Survey survey = (Survey) session.getAttribute("survey");
    	
		String button = request.getParameter("button");
		
		if (button == null) {
			return "createsurvey.jsp";
		}

		if (button.equals("Cancel")) {
			try {
				surveyDAO.delete(survey.getSid());
				Survey[] old_survey = surveyDAO.getUserSurveys(user.getUserid());
				synchronized(session) {
					session.removeAttribute("survey");
					session.setAttribute("survey_list", old_survey);
				}
				return "researcher.jsp";
			} catch (DAOException e) {
				errors.add(e.getMessage());
				return "errors.jsp";
			}
		}
		
		if (button.equals("Go Back to List of Questions")) {
			return "surveysummary.jsp";
		}
		
		if (button.equals("Save Progress")) {
			synchronized(session) {
				session.setAttribute("survey", null);
			}
			return "researcher.jsp";
		}
		
		if (button.equals("Import Entity Data")) {
			synchronized(session) {
				session.setAttribute("pageFrom", "createsurvey.jsp");
				session.setAttribute("importentitybody", "init");
			}
			return "importentity.jsp";
		}
		
		if (button.equals("Associate Mailing List")) {
			try {
				MailingList[] ml = mlDAO.getMailingListByUserid(user.getUserid());
				if (ml == null || ml.length == 0) {
					errors.add("There are no mailing list associated with your account, please go back and create one.");
					return "createsurvey.jsp";
				}
				synchronized(session) {
					session.setAttribute("mailinglistbody", "associate");
					session.setAttribute("mailinglist", ml);
					session.setAttribute("pageFrom", "createsurvey.jsp");
				}
				return "mailinglist.jsp";
			} catch (DAOException e) {
				errors.add(e.getMessage());
				return "errors.jsp";
			}
		}

		if (button.equals("Binary") || button.equals("Numeric") || button.equals("Open Ended") || button.equals("Likert") ||
				button.equals("Multiple Choice") || button.equals("Multiple Selection") || button.equals("Rank Order") ||
				button.equals("Continuous") || button.equals("Network Binary List") ||
				button.equals("Network Open List") || button.equals("Network Weighted List") || 
				button.equals("Network Weighted Continuous List") || button.equals("Network Rank Order") || 
				button.equals("Socio-cognitive Network List")) {
			try {
				Question question = new Question(questionDAO.getQid());
				question.setSid(survey.getSid());
				question.setEntityFrom("");
				question.setEntityTo("");
				
				Integer qnum = (Integer) session.getAttribute("questionmodify");
				if (qnum == null)
					question.setQnum(questionDAO.getMaxQnumBySid(survey.getSid()) + 1);
				else
					question.setQnum(qnum);
				
				synchronized(session) {
					session.setAttribute("network", "false");
					session.removeAttribute("questionmodify");
				}
				
				if (button.equals("Binary")) {
					question.setType(QuestionType.BINARY);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
					}
					return "binaryq.jsp";
				}
				else if (button.equals("Numeric")) {
					question.setType(QuestionType.NUMERIC);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
					}
					return "numericq.jsp";
				}
				else if (button.equals("Open Ended")) {
					question.setType(QuestionType.OPEN_ENDED);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
					}
					return "openendedq.jsp";
				}
				else if (button.equals("Likert")) {
					question.setType(QuestionType.LIKERT);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
						session.setAttribute("likert", "first");
					}
					return "likertq.jsp";
				}
				else if (button.equals("Multiple Choice")) {
					question.setType(QuestionType.MULTIPLE_CHOICE);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
						session.setAttribute("multiplechoice", "1");
					}
					return "multiplechoiceq.jsp";
				}
				else if (button.equals("Multiple Selection")) {
					question.setType(QuestionType.MULTIPLE_SELECTION);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
						session.setAttribute("multiplechoice", "1");
					}
					return "multiplechoiceq.jsp";
				}
				else if (button.equals("Rank Order")) {
					question.setType(QuestionType.RANK_ORDER);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
						session.setAttribute("multiplechoice", "1");
					}
					return "multiplechoiceq.jsp";
				}
				else if (button.equals("Continuous")) {
					question.setType(QuestionType.CONTINUOUS);
					questionDAO.create(question, surveyDAO, ecDAO);
					synchronized(session) {
						session.setAttribute("question", question);
					}
					return "continuousq.jsp";
				}
				
				//TODO: Prompt for Entity before choosing Network questions
				
				//If code reached here, the question must be network type questions
				else {
					EntityClass[] ec = ecDAO.getEntityClassBySid(survey.getSid());
					synchronized(session) {
						session.setAttribute("entityclass", ec);
						session.setAttribute("network", "true");
					}
					if (button.equals("Network Binary List")) {
						question.setType(QuestionType.NETWORK_BINARY);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
						}
						return "binaryq.jsp";
					}
					else if (button.equals("Network Open List")) {
						question.setType(QuestionType.NETWORK_OPEN);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
						}
						return "openendedq.jsp";
					}
					else if (button.equals("Network Weighted List")) {
						question.setType(QuestionType.NETWORK_WEIGHTED);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
							session.setAttribute("multiplechoice", "1");
						}
						return "multiplechoiceq.jsp";
					}
					else if (button.equals("Network Weighted Continuous List")) {
						question.setType(QuestionType.NETWORK_WEIGHTED_CONTINUOUS);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
						}
						return "continuousq.jsp";
					}
					else if (button.equals("Network Rank Order")) {
						question.setType(QuestionType.NETWORK_RANK_ORDER);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
							session.setAttribute("multiplechoice", "1");
						}
						return "multiplechoiceq.jsp";
					}
					else if (button.equals("Socio-cognitive Network List")) {
						question.setType(QuestionType.NETWORK_SOCIO_COGNITIVE);
						questionDAO.create(question, surveyDAO, ecDAO);
						synchronized(session) {
							session.setAttribute("question", question);
						}
						return "sociocognitiveq.jsp";
					}
				}
			} catch (DAOException e) {
				errors.add(e.getMessage());
				return "errors.jsp";
			}
		}

		errors.add("Illegal button value of " + button);
		return "errors.jsp";
    }
}
